import { md5 } from "@/util/md5";
import { navigate } from "@/util/navigate";
import { request, catchException } from "@/util/request";
import { formatDate, stringifyParams } from "@/util/util";

async function singleSign(item: any) {
  try {
    let obj = { url: "", params: "" };
    const special = {};
    if (item.url.startsWith("#小程序://")) {
      const params = parseUrl(item.url);
      const { path = "", appId = "" } = params as any;
      try {
        await uni.navigateToMiniProgram({ path, appId, extraData: {} });
      } catch (error) {}
    } else {
      for (const key in special) {
        if (special[key].includes(item.url)) obj = await formatParams(key);
      }
      navigate({
        navMethod: "navigate",
        url: "/pages/webView/index",
        query: { src: obj.url || item.url, params: obj.params },
      });
    }
  } catch (error) {
    catchException(error);
  }
}

function parseUrl(url: string) {
  let params = url.substring(url.lastIndexOf("?") + 1);
  let obj = {};
  const reg = /([^?&=]+)=([^?&=]*)/g;
  params.replace(reg, function (rs, $1, $2) {
    let key = decodeURIComponent($1);
    let value = decodeURIComponent($2) + "";
    obj[key] = value;
    return rs;
  });
  return obj;
}
async function formatParams(key: string) {
  let obj = { url: "", params: "" };
  switch (key) {
  }
  return obj;
}

export { singleSign };
